package com.example.yangxu0703.thundermapclient.adapter;

import android.content.Context;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.yangxu0703.thundermapclient.R;
import com.example.yangxu0703.thundermapclient.fragment.PictureFragment;
import com.example.yangxu0703.thundermapclient.fragment.VedioFragment;
import com.facebook.drawee.view.SimpleDraweeView;
import com.truizlop.sectionedrecyclerview.SectionedRecyclerViewAdapter;
import java.util.ArrayList;


/**
 * Created by administrator on 2017/10/19.
 */

public class VedioAdapter extends SectionedRecyclerViewAdapter<VedioAdapter.MyHeadView, VedioAdapter.MyViewHolder, VedioAdapter.MyFootView> {


    Context context;
    public ArrayList<ArrayList<String>> image_date;
    public boolean[][] vedio_status;
    OnItemClickListener onItemClickListener;
    public  ArrayList<String> deleteList;
    public boolean Mode;
    private String TAG="VedioAdapter";
    public void setMode(boolean mode) {
        Mode = mode;
    }
    public float desity;

    public void  setVedioDate(ArrayList<ArrayList<String>> image_date,boolean [][] vedio_status){
        this.image_date = image_date;
        this.vedio_status=vedio_status;
        this.notifyDataSetChanged();
    }

    public VedioAdapter(ArrayList<ArrayList<String>> image_date,boolean[][] vedio_status, Context context) {
        this.context = context;
        this.image_date = image_date;
        Log.i("Adapter", image_date + "con");
        this.vedio_status=vedio_status;
        desity = context.getResources().getDisplayMetrics().density;
        deleteList = new ArrayList<>();
    }

    @Override
    protected MyFootView onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    protected MyHeadView onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.headitem, parent, false);

        return new MyHeadView(view);
    }

    @Override
    protected MyViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_vedio, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(MyViewHolder holder, final int section, final int position) {
        final MyViewHolder viewHolder = (MyViewHolder) holder;
        if(Mode){
            viewHolder.checkBox.setVisibility(View.VISIBLE);
            Log.i(TAG,"onBindItemViewHolder ---Mode");
            if (vedio_status[section][position]) {
                viewHolder.checkBox.setBackground(context.getResources().getDrawable(R.drawable.choosed_icon));
            } else {
                viewHolder.checkBox.setBackground(context.getResources().getDrawable(R.drawable.cycle));
            }
        }else {
            viewHolder.checkBox.setVisibility(View.GONE);
            Log.i(TAG,"onBindItemViewHolder---GONE");
        }


        viewHolder.simpleDraweeView.setImageBitmap(ThumbnailUtils.createVideoThumbnail(image_date.get(section).get(position), MediaStore.Images.Thumbnails.MINI_KIND));

        viewHolder.simpleDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(section,position);
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener itemClick) {
        this.onItemClickListener = itemClick;
    }

    public void setList(ArrayList<ArrayList<String>> image_date) {
            this.image_date=image_date;
    }

    public interface OnItemClickListener {
        void onItemClick(int section, int position);
    }


    @Override
    protected void onBindSectionFooterViewHolder(MyFootView holder, int section) {

    }

    @Override
    protected void onBindSectionHeaderViewHolder(MyHeadView holder, int section) {
        MyHeadView viewHolder = (MyHeadView) holder;
        String[] tem = image_date.get(section).get(0).split("_");
        viewHolder.dateTV.setText(tem[1]);
    }

    @Override
    protected boolean hasFooterInSection(int section) {
        return false;
    }

    @Override
    public int getSectionCount() {
        return VedioFragment.listVedio.size();
    }

    @Override
    public int getItemCount() {
        return caculateSize(VedioFragment.listVedio) + VedioFragment.listVedio.size();
    }

    @Override
    protected int getItemCountForSection(int section) {
        return getCountInsection(section);
    }

    public int getCountInsection(int section) {
        Log.i("Adapter", "getCountInsection" +VedioFragment.listVedio.get(section).size());
        return  VedioFragment.listVedio.get(section).size();
    }

    public int caculateSize(ArrayList<ArrayList<String>> path) {
        int tem = 0;
        for (ArrayList<String> list : path) {
            tem = tem + list.size();
        }
        Log.i("Adapter", tem + "Count_Size");
        return tem;
    }


    class MyViewHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView simpleDraweeView;
        TextView checkBox;
        ImageView start;

        public MyViewHolder(View itemView) {
            super(itemView);
            simpleDraweeView = itemView.findViewById(R.id.res_vedio);
            checkBox = itemView.findViewById(R.id.check_vedio);
            start = itemView.findViewById(R.id.vedio_icon);
        }
    }

    class MyHeadView extends RecyclerView.ViewHolder {
        TextView dateTV;

        public MyHeadView(View itemView) {
            super(itemView);
            dateTV = (TextView) itemView.findViewById(R.id.headTV);
        }
    }

    class MyFootView extends RecyclerView.ViewHolder {
        public MyFootView(View itemView) {
            super(itemView);
        }
    }
}
